/*
 * Copyright (c) 2023 huipei.x
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.xphsc.kubernetes.core.executor;


import cn.xphsc.kubernetes.core.query.DeleteOptionsBody;
import cn.xphsc.kubernetes.core.query.PatchNamespaceBody;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.AppsV1Api;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import org.apache.commons.lang3.StringUtils;

/**
 * {@link }
 * @author <a href="xiongpeih@163.com">huipei.x</a>
 * @description: 用于获取Kubernetes中的所有Patch api Executor
 * @since 1.0.0
 */
public class PatchNamespaceExecutor<T> extends AbstractExecutor<T> {

    private String patchOptionName;
    private  T entity;
    private  CoreV1Api  coreV1Api;
    private PatchNamespaceBody patchNamespaceBody;
    private AppsV1Api appsv1api;

    public PatchNamespaceExecutor(ApiClient client, PatchNamespaceBody patchNamespaceBody, PatchOptionMode  patchOptionMode) {
        super(client);
        this.patchOptionName=patchOptionMode.name();
        this.coreV1Api=new CoreV1Api(this.getClient());
        this.appsv1api=new AppsV1Api(this.getClient());
        this.patchNamespaceBody=patchNamespaceBody;
    }

    @Override
    protected  T doExecute() {
        if (StringUtils.isNotBlank(patchOptionName)) {
            switch (patchOptionName){
                case "NS":
                    try {
                        entity = (T) coreV1Api.patchNamespace(patchNamespaceBody.name(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SERVICE":
                    try {
                        entity = (T) coreV1Api.patchNamespacedService(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "DEPLY":
                    try {
                        entity = (T) appsv1api.patchNamespacedDeployment(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SECRET":
                    try {
                        entity = (T) coreV1Api.patchNamespacedSecret(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "CONFIGMAP":
                    try {
                        entity = (T) coreV1Api.patchNamespacedConfigMap(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ENDPOINTS":
                    try {
                        entity = (T) coreV1Api.patchNamespacedEndpoints(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "POD":
                    try {
                        entity = (T) coreV1Api.patchNamespacedPod(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PODTEMPLATE":
                    try {
                        entity = (T) coreV1Api.patchNamespacedPodTemplate(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SERVICEACCOUNT":
                    try {
                        entity = (T) coreV1Api.patchNamespacedServiceAccount(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PERSISTENTVOLUME":
                    try {
                        entity = (T) coreV1Api.patchPersistentVolume(patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "NODE":
                    try {
                        entity = (T) coreV1Api.patchNode(patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "STATUS":
                    try {
                        entity = (T) coreV1Api.patchNamespaceStatus(patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "DAEMONSET":
                    try {
                        entity = (T) appsv1api.patchNamespacedDaemonSet(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "REPLICASET":
                    try {
                        entity = (T) appsv1api.patchNamespacedReplicaSet(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "STATEFULSET":
                    try {
                        entity = (T) appsv1api.patchNamespacedStatefulSet(patchNamespaceBody.name(),patchNamespaceBody.namespace(),patchNamespaceBody.body(),patchNamespaceBody.pretty(),patchNamespaceBody.dryRun(),patchNamespaceBody.fieldManager(),patchNamespaceBody.fieldValidation(),patchNamespaceBody.force());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                default:

            }
        }
        return (T) entity;
    }
    public static enum PatchOptionMode {
        /**
         *
         */
        NS,
        SERVICE,
        SERVICEACCOUNT,
        ENDPOINTS,
        DEPLY,
        SECRET,
        CONFIGMAP,
        POD,
        PODTEMPLATE,
        PERSISTENTVOLUME,
        DAEMONSET,
        REPLICASET,
        STATEFULSET,
        NODE,
        STATUS,
        AUTO;
        private PatchOptionMode() {

        }
        private PatchOptionMode(String name) {
            this.name=name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}


